package cn.signalpha.modules.shop.service;

import cn.signalpha.modules.shop.entity.UserAddress;
import cn.signalpha.modules.shop.mapper.UserAddressMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


/**
 * 用户收货地址 服务类
 *
 * @author suguangjun
 */
@Service
public class UserAddressService extends ServiceImpl<UserAddressMapper, UserAddress> {

	/**
	 * 清除用户默认地址
	 */
	public void cleanUserDefaultAddress(Long userId) {
		update(UserAddress.builder().isDefault(0).build(), Wrappers.<UserAddress>lambdaQuery().eq(UserAddress::getUserId, userId).eq(UserAddress::getIsDefault, 1));
	}

	/**
	 * 获取用户默认地址
	 */
	public UserAddress getDefaultUserAddress(Long userId) {
		return this.baseMapper.selectOne(Wrappers.<UserAddress>lambdaQuery().eq(UserAddress::getUserId, userId).eq(UserAddress::getIsDefault, 1));
	}

	/**
	 * 获取用户地址
	 */
	public UserAddress getByUserId(Long addrId, Long userId) {
//		if(addrId == 0){
//			UserAddress userAddress = this.getDefaultUserAddress(userId);
//			if(userAddress == null){
//				userAddress = baseMapper.selectOne(Wrappers.<UserAddress>lambdaQuery().eq(UserAddress::getUserId, userId).last("limit 1"));
//			}
//			return userAddress;
//		}
		return this.baseMapper.selectById(addrId);
	}

}
